package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Approval;
import com.zyn.rsgl.pojo.Reimbursement;
import com.zyn.rsgl.pojo.ScheduleDetail;
import com.zyn.rsgl.pojo.Sign;
import com.zyn.rsgl.service.ReimbursementService;
import com.zyn.rsgl.service.ScheduleService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 补卡表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/rsgl/reimbursement")
public class ReimbursementController {
    @Resource
    private ReimbursementService reimbursementService;
    @Resource
    private SignController signController;
    @Resource
    private ApprovalController approvalController;

    // 分页查询
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String userId
    ) {
        return reimbursementService.listPage(pageNum, pageSize, startTime, status, userId);
    }

    // 获取单个补卡信息
    @GetMapping("/get/{reimbursementId}")
    public R get(@PathVariable("reimbursementId") String reimbursementId) {
        Reimbursement byId = reimbursementService.getById(reimbursementId);
        if (byId != null)
            return R.ok(byId);
        return R.fail("补卡信息不存在");
    }

    // 添加补卡信息
    @PostMapping("/add")
    public R add(@RequestBody Reimbursement reimbursement) {
//        ScheduleDetail scheduleDetail = scheduleService.getScheduleDetailByDate(reimbursement.getUserId(), reimbursement.getStartTime());
//        if (scheduleDetail == null || !scheduleDetail.getIsWorkday()) {
//            return R.fail("当天没有排班信息");
//        }

        // 判断是否存在
        if (reimbursementService.getById(reimbursement.getId()) != null)
            return R.fail("补卡信息已存在");
        reimbursement.setStatus("待审批");

        boolean isSuccess = reimbursementService.save(reimbursement);
        if(isSuccess){
            Approval approval = new Approval();
            approval.setOriginalId(reimbursement.getId());
            approval.setUserId(reimbursement.getUserId());
            approval.setType("补卡");
            approval.setReason(reimbursement.getReason());
            approval.setStartTime(reimbursement.getStartTime());
            approval.setReType(reimbursement.getReType());
            approval.setStatus("待审批");
            approvalController.add(approval);
            return R.ok();
        }
        return R.fail("添加失败");
    }

    // 修改补卡信息
    @PutMapping("/update")
    public R update(@RequestBody Reimbursement reimbursement) {
        if("已批准".equals(reimbursement.getStatus() )){
            Sign sign = new Sign();
            sign.setPunchType(reimbursement.getReType());
            sign.setDate(reimbursement.getStartTime());
            sign.setUserId(reimbursement.getUserId());
            sign.setOrigin("补卡");
            signController.add(sign);
        }
        return reimbursementService.updateById(reimbursement) ? R.ok() : R.fail("修改失败");
    }

    // 删除补卡信息
    @DeleteMapping("/delete/{reimbursementId}")
    public R delete(@PathVariable("reimbursementId") String reimbursementId) {
        return reimbursementService.removeById(reimbursementId) ? R.ok() : R.fail("删除失败");
    }
}
